﻿using System;
using System.IO;
using System.Net.Http;
using System.Drawing;
using System.Net;
using System.Reflection;

namespace WinApp.Until
{
    public class ImageHelper
    {
        /// <summary>
        /// 根据网络地址获取图片
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <returns></returns>
        public static Image? LoadImageFromWebUriAsync(string imageUrl)
        {
            try
            {
                Image result = null;

                if (string.IsNullOrEmpty(imageUrl))
                {
                    return null;
                    //throw new ArgumentException("ImageUrl cannot be null or empty.");
                }

                using (var webC = new WebClient())
                {
                    using (var streamFilePic = webC.OpenRead(imageUrl))
                    {
                        result = Image.FromStream(streamFilePic);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return null;
            }

        }

        public static Image? loadImageFromLocalPath(string path)
        {
            try
            {

                // 检查文件是否存在
                if (!File.Exists(path))
                {
                    throw new FileNotFoundException($"无法找到图片文件：{path}");
                }

                //string imagePath = @"C:\Users\YourUsername\Pictures\image.jpg"; // 替换为你的图片路径
                Image image = Image.FromFile(path);
                return image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
    }
}
